using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlTypes;

using Destra.Tools.DACGenerator.Config;

namespace Destra.Tools.DACGenerator
{
	public partial class ParameterOverrideDialog : Form
	{
		public class ParameterRow
		{
			private string m_name;
			private SqlDbType m_sqlDbType;
			private bool m_nullable;
			private QueryInfo.Parameter m_parameter;

			public ParameterRow(QueryInfo.Parameter parameter)
			{
				m_parameter = parameter;
			}

			public QueryInfo.Parameter Parameter
			{
				get
				{
					return m_parameter;
				}
			}

			public string Name
			{
				get
				{
					return m_name;
				}
				set
				{
					m_name = value;
				}
			}

			public SqlDbType SqlDbType
			{
				get
				{
					return m_sqlDbType;
				}
				set
				{
					m_sqlDbType = value;
				}
			}

			public bool Nullable
			{
				get
				{
					return m_nullable;
				}
				set
				{
					m_nullable = value;
				}
			}
		}

		private ParameterConfig[] m_parameterConfigs;
		private List<QueryInfo.Parameter> m_parameters;
		private List<ParameterRow> m_rows;

		public ParameterOverrideDialog(ParameterConfig[] parameterConfigs, List<QueryInfo.Parameter> parameters)
		{
			InitializeComponent();

			m_parameterConfigs = parameterConfigs;
			m_parameters = parameters;
		}

		private void ParameterOverrideDialog_Load(object sender, EventArgs e)
		{
			dataGridView.AutoGenerateColumns = false;
			dataGridView.AllowUserToAddRows = false;
			dataGridView.AllowUserToDeleteRows = false;
			dataGridView.RowHeadersVisible = false;

			DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
			nameColumn.Name = "Name";
			nameColumn.HeaderText = "Name";
			nameColumn.ReadOnly = true;
			nameColumn.DataPropertyName = "Name";
			dataGridView.Columns.Add(nameColumn);
			
			DataGridViewComboBoxColumn typeColumn = new DataGridViewComboBoxColumn();
			typeColumn.Name = "SqlDbType";
			typeColumn.HeaderText = "SqlDbType";
			typeColumn.DataPropertyName = "SqlDbType";
			foreach (SqlDbType sqlDbType in Enum.GetValues(typeof(SqlDbType)))
			{
				typeColumn.Items.Add(sqlDbType);
			}
			dataGridView.Columns.Add(typeColumn);

			DataGridViewComboBoxColumn nullableColumn = new DataGridViewComboBoxColumn();
			nullableColumn.Name = "Nullable";
			nullableColumn.HeaderText = "Nullable";
			nullableColumn.DataPropertyName = "Nullable";
			nullableColumn.Items.Add(true);
			nullableColumn.Items.Add(false);
			dataGridView.Columns.Add(nullableColumn);

			m_rows = new List<ParameterRow>();

			foreach (QueryInfo.Parameter parameter in m_parameters)
			{
				ParameterRow row = new ParameterRow(parameter);
				row.Name = parameter.Name;
				row.SqlDbType = parameter.SqlDbType;
				row.Nullable = parameter.Nullable;

				if (m_parameterConfigs != null)
				{
					foreach (ParameterConfig parameterConfig in m_parameterConfigs)
					{
						if (parameter.Name.Equals(parameterConfig.Name))
						{
							row.SqlDbType = parameterConfig.SqlDbType;
							row.Nullable = parameterConfig.Nullable;

							break;
						}
					}
				}

				m_rows.Add(row);
			}
			dataGridView.DataSource = m_rows;
		}

		private void saveButton_Click(object sender, EventArgs e)
		{
			foreach (ParameterRow parameterRow in m_rows)
			{
				QueryInfo.Parameter parameter = parameterRow.Parameter;

				parameter.SqlDbType = parameterRow.SqlDbType;
				parameter.Nullable = parameterRow.Nullable;
			}
		}

		private void resetButton_Click(object sender, EventArgs e)
		{
			foreach (ParameterRow parameterRow in m_rows)
			{
				parameterRow.SqlDbType = parameterRow.Parameter.SqlDbType;
				parameterRow.Nullable = parameterRow.Parameter.Nullable;
			}

			dataGridView.Refresh();
		}
	}
}